package com.hyfd.deal.Flow;

import java.security.MessageDigest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.hyfd.common.GenerateData;
import com.hyfd.common.https.HttpsUtils;
import com.hyfd.common.utils.DateTimeUtils;
import com.hyfd.common.utils.XmlUtils;
import com.hyfd.deal.BaseDeal;

public class FuYuanFlowDeal implements BaseDeal{

	Logger log = Logger.getLogger(FuYuanFlowDeal.class);
	
	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> deal(Map<String, Object> order) {
		Map<String,Object> map = new HashMap<String,Object>();
		int flag = -1;
		try{
			String phone = (String) order.get("phone");
			String value = new Double((String)order.get("value")).intValue()+"M";
			if(value.equals("1024")){
				value = "1G";
			}else if(value.equals("1536")){
				value = "1.5G";
			}
			Map<String,Object> channel = (Map<String, Object>) order.get("channel");//获取通道参数
			String defaultParameter = (String) channel.get("default_parameter");//默认参数
			Map<String,String> paramMap = XmlUtils.readXmlToMap(defaultParameter);
			String linkUrl = (String) channel.get("link_url");
			String key = (String) channel.get("key");
			String agentid = paramMap.get("agentid");
			String orderid = agentid+DateTimeUtils.formatDate(new Date(), "yyyyMMddHHmmss") + GenerateData.getIntData(9, 7);
			map.put("orderId", orderid);
			Map<String,Object> pkg = (Map<String, Object>) order.get("pkg");
			String dataType = (String) pkg.get("data_type");//适用范围，1是省内，2是国内
			String Userange = dataType.equals("1")?"1":"0";
			String sign = getSign(agentid,phone,value,orderid,Userange,key);
			String param = "action=refer&agentid=" + agentid + "&mobilenum=" + phone + "&norm=" + value + "&userange=" + Userange + "&orderid=" + orderid + "&sign=" + sign;
			String result = HttpsUtils.postParameters(linkUrl, param);
			JSONObject resultJson = JSONObject.parseObject(result);
			String data = resultJson.getString("Data");
			String status = resultJson.getString("Status");
			map.put("result", data+":"+status);
			if(status.equals("Success")){//成功
				flag = 1;
			}else{
				flag = 0;
			}
		}catch(Exception e){
			log.error("福缘流量充值接口出错"+e.getMessage());
		}
		map.put("status", flag);
		return map;
	}

	/**
	 * 获取签名
	 * @author lks 2017年9月5日下午4:57:46
	 * @param agentid
	 * @param mobilenum
	 * @param norm
	 * @param orderid
	 * @param userange
	 * @param key
	 * @return
	 */
	public static String getSign(String agentid, String mobilenum, String norm, String orderid, String userange, String key){
		//agentid=xxxx&mobilenum=xxxx&norm=xxxx&orderid=xxxx&userange=xxxx&key=xxxx
		//agentid={0}&mobilenum={1}&norm={2}&orderid={3}&userange={4}&key={5}
		String signStr = "agentid="+agentid+"&mobilenum="+mobilenum+"&norm="+norm+"&orderid="+orderid+"&userange="+userange+"&key="+key;
		String sign = MD5(signStr);
		return sign.toLowerCase();
	}
	
	public static String MD5(String s) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'a', 'b', 'c', 'd', 'e', 'f' };
		try {
			byte[] btInput = s.getBytes("UTF-8");
			MessageDigest mdInst = MessageDigest.getInstance("MD5");
			mdInst.update(btInput);
			byte[] md = mdInst.digest();
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str).toUpperCase();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
}
